//	BioloidBiped.h

#ifndef BIOLOIDBIPED_h
#define BIOLOIDBIPED_h

#include <avr/pgmspace.h>


//	Events which are posted to the application.
#define BBEV_INITIALISED	1
#define BBEV_GAIT_COMPLETED	2	//	The gait has stopped so the robot can start a new move
#define BBEV_STEP			3	//	The robot has taken a further step
#define BBEV_INITIAL_STEP	4	//	The robot has completed an initial half step

void BBInitialise(void (*ReportFn)(int Event));
	//	Call this function once on start-up.
	//	ReportFn can be used to hear about the BBEV_... events.  Set it to NULL if not required.
	
void BBNulReportFn (int Event);
	//	This is the default event reporter.  It does nothing.

void BBHeartBeat(void);
	//	Call this 128 times per second

void BBStop(void);
	//	Call to stop any current motion. Do not assume that the motion will stop immediately, rather the code
	//	must wait for a BBEV_GAIT_COMPLETED event.
	
void BBWalkForwards(int Rpc, int Lpc, int Flags);
	//	Call to walk forwards.
	//	Rpc & Lpc are right & left step percentages.  I.e. (100, 100) is walk straight forward at full speed,
	//	(100, 50) will cause an arc to the left.
	//	Can be called while walking, then just updates Rpc & Lpc.
	//	See BBF_... for Flags values.

char *BBEventText(int Event, char *Text);

//	The following joint IDs are labelled from 0; add 1 to get servo IDs
#define	JID_R_SHDR_FORE		0	//	Shoulder fore/aft servo
#define	JID_L_SHDR_FORE		1
#define	JID_R_SHDR_SPLAY	2	//	Shoulder splay
#define	JID_L_SHDR_SPLAY	3
#define	JID_R_ELBOW			4	//	Elbows are rotated by 90 degrees?!
#define	JID_L_ELBOW			5
#define	JID_R_HIP_YAW		6	//	Yaw movement in hips
#define	JID_L_HIP_YAW		7
#define	JID_R_HIP_SPLAY		8	//	Leg splay
#define	JID_L_HIP_SPLAY		9
#define	JID_R_HIP_FORE		10	//	Hip fore/aft movement
#define	JID_L_HIP_FORE		11
#define	JID_R_KNEE			12	//	Knees
#define	JID_L_KNEE			13
#define	JID_R_ANKL_FORE		14	//	Ankle fore/aft movement
#define	JID_L_ANKL_FORE		15
#define	JID_R_ANKL_ROLL		16	//	Ankle roll movement
#define	JID_L_ANKL_ROLL		17

#define GP_IDLE				0	//	Gait Phase defines
#define GP_INITIALISE		1
#define GP_INITIAL_STEP		2
#define GP_HALF_STEP		3
#define GP_FINAL_STEP		4

//	GaitFlag defines.  Note these are bit-wise values
#define BBF_STOP_REQUESTED	0x01	//	Make true to request the gait to come to a halt. (Better to call BBStop())
#define BBF_REVERSE_STRIDE	0x02	//	Typically the gait leads wit the right foot.  Make this true to lead with the left.

//	The AX-12 manual states that a maximum speed setting of 0x3FF generates 114 RPM
//	This equates to 684 degrees/sec or approx 1.46ms per degree
//	However the AX-12 only moves through 300 degrees, which should take 438ms (assuming infinite acceleration).
//	The 300 degrees equates to an input of 1024 units, so a move of 1024 units should take 438ms.
//	A heartbeat takes 1/128 = 7.8125ms
//	So MoveRequired/NumHeartBeats * 438/7.8125 should yield servo speed to use.
//	438/7.8125 = approx 56.  This is defined below for speed calculations
//	Also, a given move of x (in the range 0-1023) will take 438/(1024 x 7.8125) heartbeats = 0.05475 heartbeats

#define SERVO_SPEED_MULTIPLIER	56
#define MAX_JOINTS				JID_L_ANKL_ROLL+1


typedef struct BB_POSE_STRUCT
{
	int						Ticks;
	unsigned short			Joint[MAX_JOINTS];
	void (*UpdateFn)(struct BB_POSE_STRUCT *Pose);
}	BB_POSE;

extern int Speed[] PROGMEM;

extern int Plane[] PROGMEM;
extern int NegPlane[] PROGMEM;
extern int CustomPoses[] PROGMEM;


//Width = 70
//Height = 75
//Size = 31500 Bytes

//extern int Speed[];


//extern int Plane[];
//512,512,512

//};

//extern int NegPlane[];
//512,512,512
/**/
//};




#endif